package com.example.gymmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Matchs;
import com.example.gymmanagement.domain.Notify;
import com.example.gymmanagement.service.NotifyService;
import com.example.gymmanagement.mapper.NotifyMapper;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 蜗牛菌
* @description 针对表【notify】的数据库操作Service实现
* @createDate 2023-06-08 16:06:47
*/
@Service
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, Notify>
    implements NotifyService {
    @Autowired
    private NotifyMapper notifyMapper;

    @Override
    public boolean insert(Notify notify) {
        boolean success = false;
        try {
            success = this.notifyMapper.insert(notify) > 0;
        } catch (Exception e) {
            success = false;
        }
        return success;
    }

    @Override
    public Result selectList(int index, int size, Notify notify) {
            if (index<=0){
                index =1;
            }
            if (size <=0){
                size =10;
            }

            Page<Notify> pageInfo = new Page(index,size);

            LambdaQueryWrapper<Notify> lambdaQueryWrapper = new LambdaQueryWrapper();
            if (notify.getNotifyId()!=null&&notify.getNotifyId()!=0&& !Objects.equals(notify.getNotifyId(), "")) {
                lambdaQueryWrapper.eq(Notify::getNotifyId,notify.getNotifyId());
            }
            if (StringUtils.isNotEmpty(notify.getNotifyTitile())&& !Objects.equals(notify.getNotifyTitile(), "")){
                lambdaQueryWrapper.like(Notify::getNotifyTitile,notify.getNotifyTitile());
            }
            if (notify.getNotifyAuthor()!=null&&notify.getNotifyAuthor()!=0&& !Objects.equals(notify.getNotifyAuthor(), "")) {
                lambdaQueryWrapper.eq(Notify::getNotifyAuthor,notify.getNotifyAuthor());
            }
            if (StringUtils.isNotEmpty(notify.getNotifyContent())&& !Objects.equals(notify.getNotifyContent(), "")){
                lambdaQueryWrapper.like(Notify::getNotifyContent,notify.getNotifyContent());
            }
            if (StringUtils.isNotEmpty(notify.getNotifyHtml())&& !Objects.equals(notify.getNotifyHtml(), "")){
                lambdaQueryWrapper.like(Notify::getNotifyHtml,notify.getNotifyHtml());
            }
            if (StringUtils.isNotEmpty(notify.getNotifyType())&& !Objects.equals(notify.getNotifyType(), "")){
                lambdaQueryWrapper.like(Notify::getNotifyType,notify.getNotifyType());
            }

            Page<Notify> notifyPage = notifyMapper.selectPage(pageInfo,lambdaQueryWrapper);

            System.out.println("total:"+notifyPage.getTotal());
            Integer code = notifyPage.getTotal()!=0? StatusCode.GET_OK:StatusCode.GET_ERR;
            String msg = notifyPage.getTotal()!=0?"查询成功":"未查询到数据";
            return new Result(code,notifyPage,"查询到的数据量为："+notifyPage.getTotal());

    }

    @Override
    public boolean deleteById(int id) {
        return this.notifyMapper.deleteById(id) >0;
    }

    @Override
    public boolean updateField(Notify notify) {
        return this.notifyMapper.updateById(notify) >0;
    }
}




